//
//  CJTitleButton.swift
//  SwiftDemo
//
//  Created by Chris on 2017/7/19.
//  Copyright © 2017年 chris. All rights reserved.
//

import UIKit


/// 首页导航栏用户信息按钮
class CJTitleButton: UIButton {
    
    init(title: String?) {
        super.init(frame: CGRect.zero)
        
        if title == nil {
            setTitle("首页", for: .normal)
        } else {
            setTitle(title! + "  ", for: .normal)
            setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: .normal)
            setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: .selected)
        }
        
        setTitleColor(UIColor.darkGray, for: .normal)
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        
        sizeToFit()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel, let imageView = imageView else {
            return
        }
        if titleLabel.frame.minX > imageView.frame.minX {
            titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
            imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
